//
//  NYSearchViewModel.m
//  Nooryes
//
//  Created by baken on 2020/3/9.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYSearchViewModel.h"
#import "NYSearchManager.h"
#import "NYTopicCategoryModel.h"
#import "NYSearchUserModel.h"
#import "NYTopicListModel.h"
#import "NYArticleListModel.h"
#import "TopicCategoryModel.h"

@interface NYSearchViewModel()

@end

@implementation NYSearchViewModel


- (void)getHotCategoryListWithCompletionHandle:(CompletionHandle)complete{
    [NYSearchManager getHotCategoryListWithPage:STR_FROM_INT(self.pageNum) andCompletionHandle:^(NYTopicCategoryModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.cateroryArray addObjectsFromArray:model.data];
            [model.data enumerateObjectsUsingBlock:^(TopicCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.cateroryTitleArray addObject:obj.name];
                
            }];
        }
        complete(error);
        //
    }];
    
}

- (void)getHotTopicListWithCompletionHandle:(CompletionHandle)complete{
    [NYSearchManager getHotTopicListWithPage:STR_FROM_INT(self.pageNum) andCompletionHandle:^(NYTopicListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.topicArray addObjectsFromArray:model.data];
        }
        self.tableViewHeight = kTopicRowHeight * self.topicArray.count;
        complete(error);
    }];
    
}

- (void)getSearchAriticlesWithCompletionHandle:(CompletionHandle)complete{
    self.artType= IsStrEmpty(self.artType)?@"default":self.artType;
    NSString *gb2312keys = [self.keys stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [NYSearchManager getSearchAriticlesWithKeys:gb2312keys andType:self.artType andPage:STR_FROM_INT(self.articlePage) andCompletionHandle:^(NYArticleListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.articlePage == 1) {
                [self.articlesArray removeAllObjects];
            }
            [self.articlesArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
    
}

- (void)getSearchTopicListWithCompletionHandle:(CompletionHandle)complete{
    self.topType= IsStrEmpty(self.topType)?@"default":self.topType;
    NSString *gb2312keys = [self.keys stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [NYSearchManager getSearchTopicListWithKeys:gb2312keys andType:self.topType andPage:STR_FROM_INT(self.topicPage) andCompletionHandle:^(NYTopicListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.topicPage == 1) {
                [self.topicArray removeAllObjects];
            }
            [self.topicArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}


- (void)getSearchUserListWithCompletionHandle:(CompletionHandle)complete{
    NSString *gb2312keys = [self.keys stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [NYSearchManager getSearchUserListWithKeys:gb2312keys andPage:STR_FROM_INT(self.userPage) andCompletionHandle:^(NYSearchUserModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.userPage == 1) {
                [self.userArray removeAllObjects];
            }
            [self.userArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}

//- (void)getSearchHistoryListWithCompletionHandle:(CompletionHandle)complete{
//    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchList"];
//    [self.historyArray addObjectsFromArray:array];
//    complete(nil);
//}

-(NSMutableArray *)topicArray{
    if (!_topicArray) {
        _topicArray = [NSMutableArray array];
    }
    return _topicArray;
}

-(NSMutableArray *)cateroryTitleArray{
    if (!_cateroryTitleArray) {
        _cateroryTitleArray = [NSMutableArray array];
    }
    return _cateroryTitleArray;
}

-(NSMutableArray *)cateroryArray{
    if (!_cateroryArray) {
        _cateroryArray = [NSMutableArray array];
    }
    return _cateroryArray;
}

-(NSMutableArray *)userArray{
    if (!_userArray) {
        _userArray = [NSMutableArray array];
    }
    return _userArray;
}


-(NSMutableArray *)articlesArray{
    if (!_articlesArray) {
        _articlesArray = [NSMutableArray array];
    }
    return _articlesArray;
}


@end
